<template>
  <!--  =>  -->
  <div class="pages refund">
    <div class="pageLevel" v-if="!sub">
      <!-- 头部 -->
      <!-- 搜索框 -->
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm" size="small">
          <el-form-item label-width="100px" label="券批次名称:" style="margin-left:10px">
            <el-input class="input" v-model="selectdata.keyword" size="small" placeholder="请输入券批次名称"></el-input>
          </el-form-item>
          <el-form-item label="发放时间:" style="margin-left:10px">
            <el-date-picker :clearable="false" format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="value" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="" prop="wxser" class="wxelForm" label-width="25px">
            <el-button type="primary" @click="toPage" size="mini">查询</el-button>
          </el-form-item>
          <el-form-item label="" label-width="55px" class="searchBtn">
            <el-button type="primary" size="mini" v-if="setRole('新增')" @click="routeradd('add')">去发券</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableMain">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="55" align="center" fixed></el-table-column>
          <el-table-column prop="ticketBatchName" label="券批次名称" align="center" fixed>
          </el-table-column>
          <el-table-column prop="ticketList" label="发放优惠券" align="center">
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.ticketList" :key="index">
                <span>{{item.ticketName}} </span>
              </div>
            </template>
          </el-table-column>
          </el-table-column>
          <el-table-column prop="sendType" label="发放方式" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.sendType==0">即时发放</span>
              <span v-if="scope.row.sendType==1">定时发放</span>
            </template>
          </el-table-column>
          <el-table-column prop="sendTime" label="发放时间" align="center" :formatter="trunTime" width="160">
          </el-table-column>
          <el-table-column prop="sendState" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.sendState==0">待发放</span>
              <span v-if="scope.row.sendState==1">已开始</span>
              <span v-if="scope.row.sendState==2">已发放</span>
              <span v-if="scope.row.sendState==3">已取消</span>
            </template>
          </el-table-column>
          <el-table-column prop="sendCustomerCount" label="发放客户量" align="center">
          </el-table-column>
          <el-table-column prop="sendTicketCount" label="发放张数" align="center">
          </el-table-column>
          <el-table-column prop="creator" label="创建人" align="center">
          </el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template slot-scope="scope">
              <el-button size="small" type="text" class="addDetail" @click="routeradd('lock',scope.row.id)">查看</el-button>
              <el-button size="small" type="text" class="addDetail" v-if="scope.row.sendState==0||scope.row.sendState==3 &&setRole('修改')" @click="routeradd('see',scope.row.id)">修改</el-button>
              <el-button size="small" type="text" class="addDetail" v-if="scope.row.sendState==0 &&setRole('取消')" @click="cancelTicketSend(scope.row.id)">取消</el-button>

            </template>
          </el-table-column>
        </el-table>
        <div class="pageblock">
          <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10,20,50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="pageLevel" v-else>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import { selectTicketSendList, cancelTicketSend } from "@/api";
import util from "@/util";
import s from "@/util/state";
import { mapState } from "vuex";
export default {
  computed: mapState(["power", "merchantId", "sub"]),
  data() {
    return {
      // pickerOptions0: {
      //   disabledDate(time) {
      //     return time.getTime() > Date.now() - 8.64e6; //如果没有后面的-8.64e6就是不可以选择今天的
      //   }
      // },
      routePath: "",
      value: [],
      status: "",
      name: "",
      pageSize: 10,
      total: 0,
      currentPage: 1,
      selectdata: {
        keyword: "",
        paymentStatus: "",
        type: ""
      },
      tableData: [],
      sum: ""
    };
  },
  watch: {
    sub: {
      handler: function(val, oldVal) {
        if (oldVal != null && val == false) {
          this.getdata(10, 1);
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getdata(10, 1);
  },
  methods: {
    lastPayType: s.lastPayType,
    routeradd(type, id) {
      util.routerTo(
        "/marketing-center/company-active/hair-coupon/hair-couponAdd",
        {
          type: type,
          id: id
        }
      );
    },
    // 取消发放
    cancelTicketSend(id) {
      let data = {
        id: id,
        sendState: 3
      };
      cancelTicketSend(data).then(res => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "取消成功!"
          });
          this.getdata(this.pageSize, 1);
        }
      });
    },
    trunTime(row, column, cellValue) {
      return util.foryear(cellValue) + " " + util.fortime(cellValue);
    },
    //配置权限
    setRole(key) {
      return util.selectTitle(this.power, key);
    },
    //跳页
    toPage() {
      this.getdata(this.pageSize, 1);
      this.currentPage = 1;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getdata(val, this.currentPage);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdata(this.pageSize, val);
    },
    // getTime() {
    //   //---------------默认当前时间
    //   let date = new Date();
    //   let y = date.getFullYear();
    //   let m = date.getMonth() + 1;
    //   let d = date.getDate();
    //   if (m >= 1 && m <= 9) {
    //     m = "0" + m;
    //   }
    //   if (d >= 1 && d <= 9) {
    //     d = "0" + d;
    //   }
    //   let time = y + "-" + m + "-" + d;
    //   this.$set(this.value, 0, time);
    //   this.$set(this.value, 1, time);
    // },
    formatTimes: function(row) {
      return util.foryear(row.createTime) + " " + util.fortime(row.createTime);
    },
    getdata(pageSize, pageNum) {
      if (!this.value) {
        this.value = ["", ""];
      }
      let postdata = {
        orgId: this.merchantId,
        ticketBatchName: this.selectdata.keyword,
        ticketSendStartTime: this.value[0],
        ticketSendEndTime: this.value[1],
        pageNo: pageNum,
        pageSize: pageSize
      };
      selectTicketSendList(postdata).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        // console.log(this.tableData[i].payment);
      });
    }
  }
};
</script>

<style  scoped>
.refund .el-input__inner {
  height: 32px;
  line-height: 32px;
}
#app {
  text-align: inherit;
}
.refund .total-income {
  color: #606266;
  width: 100%;
  line-height: 55px;
  margin: 20px 0px 10px 0px;
  border-bottom: 1px solid #eee;
}
.refund .total-income .all-income {
  margin-left: 30px;
}
.refund .total-income .all-income span {
  margin-left: 10px;
}
.refund .tableSearch .el-select {
  width: 140px;
}
.refund .span1 {
  color: red;
  margin-left: 100px;
  font-size: 14px;
  font-weight: bold;
}
.wxser {
  line-height: 50px;
  margin-left: 10px;
  height: 40px;
  width: 420px;
}
.wxser div {
  float: left;
  width: 200px;
}
.wxser .span-left {
  margin-left: 20px;
}
.tips {
  text-align: center;
  margin: 30px 30px 0px 30px;
}
.searchBtn {
  float: right;
  margin-right: 50px;
}
</style>
